<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-position="left"
      label-width="110px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="userId">
        <el-input
          v-model="ruleForm.userId"
          placeholder="输入12位数字账号"
        ></el-input>
        <p>推荐使用11位手机号+单个数字组合</p>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          placeholder="输入密码"
          type="password"
        ></el-input>
        <p>6~18个字符,可使用字母、数字、下划线</p>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkpassword">
        <el-input
          type="password"
          v-model="ruleForm.checkpassword"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册并登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    //校验账号
    var validateId = (rule, value, callback) => {
      let reg = /^\d{12}$/g;
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (!reg.test(value)) {
        callback(new Error("请输入12位数字"));
      } else {
        callback();
      }
    };
    //校验密码
    var validatePassword = (rule, value, callback) => {
      let reg = /^[0-9a-zA-Z_]{6,18}$/g;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!reg.test(value)) {
        callback(new Error("密码设置非法，请重新输入"));
      } else {
        callback();
      }
    };
    //校验确认密码
    var validateCheckpassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userId: "",
        phone: "",
        password: "",
        checkpassword: "",
      },
      rules: {
        userId: [{ required: true, validator: validateId, trigger: "blur" }],
        password: [
          { required: true, validator: validatePassword, trigger: "blur" },
        ],
        checkpassword: [
          { required: true, validator: validateCheckpassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open() {
      //弹窗
      this.$alert("账号或密码不完整，请检查后在输入！", "", {
        confirmButtonText: "确定",
      });
    },
    async submit() {
      try {
        console.log("ruleForm", this.ruleForm);
        await this.$store.dispatch("user/userRegister", this.ruleForm);
        this.$router.push({ path: "/" });
      } catch (error) {
        console.log("失败");
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submit();
        } else {
          this.open();
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/ .el-form-item__label {
  text-align: center;
}
.el-form-item p {
  font-size: 18px;
  color: rgba(185, 190, 195, 100);
  margin: 0;
  font-family: SourceHanSansSC-regular;
}
</style>